package com.hxzy.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hxzy.tms.HibernateSessionHolder;

public class HibernateBaseDao<T extends Serializable> extends HibernateDaoSupport  implements BaseDao<T> {

	private Logger logger = LoggerFactory.getLogger(getClass());

    protected Class<? extends T> daoType;
   // private Session session;
    
    public HibernateBaseDao() {
		// TODO Auto-generated constructor stub
       // getSession()=HibernateSessionHolder.get();
//        this.getSession().getClass();
	}
	public T create(T t) {
		// TODO Auto-generated method stub
		this.getSession().save(t);
		return t;
	}

	public List<T> read(String hql, Map<String, Object> param) {
		// TODO Auto-generated method stub
		Query query=this.getSession().createQuery(hql);
		for(Map.Entry<String, Object> p : param.entrySet()){
			if(p.getValue() instanceof Long){
				query.setLong(p.getKey(), (Long) p.getValue());
			}else{
			query.setParameter(p.getKey(), p.getValue());
			}
		}
		return query.list();
	}

	public T read(Class<T> t, long id) {
		// TODO Auto-generated method stub
		return (T) this.getSession().get(t, id);
	}

	public int update(T t) {
		// TODO Auto-generated method stub
		try{
		this.getSession().update(t);
		return 0;
		}catch(HibernateException e){
			logger.error("更新错误 {}",e);
			return 1;
		}
	}

	public int delete(T t) {
		// TODO Auto-generated method stub
		try{
			this.getSession().delete(t);
			return 0;
			}catch(HibernateException e){
				logger.error("删除错误 {}",e);
				return 1;
			}
	}
    public List<T> read(String hql, com.hxzy.dao.BaseDao.Params... params) {
        // TODO Auto-generated method stub
        return null;
    }
  
   
}
